<?php

class ClassCourseContentLikeUnlikeController extends GxController {

    public function actionsavelikeUnlikeClassCourseContent() {
        $model = new ClassCourseContentLikeUnlike;
        $model->setAttribute('ip_address', $_SERVER['REMOTE_ADDR']);
        if ($_POST['type'] == SkillConfig::DESCRIPTION_CLASS_COURSE) {

            ($_POST['status'] == 1) ? $model->setAttribute('description_like', $_POST['status']) : $model->setAttribute('description_unlike', $_POST['status']);
            $description = ($_POST['status'] == 1) ? 'description_like' : 'description_unlike';
        }
        if ($_POST['type'] == SkillConfig::STRUCTURE_CLASS_COURSE) {
            ($_POST['status'] == 1) ? $model->setAttribute('structure_like', $_POST['status']) : $model->setAttribute('structure_unlike', $_POST['status']);
            $structure = ($_POST['status'] == 1) ? 'structure_like' : 'structure_unlike';
        }
        if ($_POST['type'] == SkillConfig::OUTCOME_CLASS_COURSE) {
            ($_POST['status'] == 1) ? $model->setAttribute('outcome_like', $_POST['status']) : $model->setAttribute('outcome_unlike', $_POST['status']);
            $outcome = ($_POST['status'] == 1) ? 'outcome_like' : 'outcome_unlike';
        }
        if ($_POST['classcoursetype'] == 'class') {
            $model->setAttribute('classes_id', $_POST['classcourseId']);
            $classVal = ClassCourseContentLikeUnlike::model()->findByAttributes(array('classes_id' => $_POST['classcourseId']));
            if (!empty($classVal->classes_id)) {
                $classVal->setAttribute('ip_address', $_SERVER['REMOTE_ADDR']);
                if ($_POST['type'] == SkillConfig::DESCRIPTION_CLASS_COURSE) {
                    ('description_like' != $description) ? $classVal->setAttribute('description_unlike', $classVal->description_unlike + 1) : $classVal->setAttribute('description_like', $classVal->description_like + 1);
                }
                if ($_POST['type'] == SkillConfig::STRUCTURE_CLASS_COURSE) {
                    ('structure_like' != $structure) ? $classVal->setAttribute('structure_unlike', $classVal->structure_unlike + 1) : $classVal->setAttribute('structure_like', $classVal->structure_like + 1);
                }
                if ($_POST['type'] == SkillConfig::OUTCOME_CLASS_COURSE) {
                    ('outcome_like' != $outcome) ? $classVal->setAttribute('outcome_unlike', $classVal->outcome_unlike + 1) : $classVal->setAttribute('outcome_like', $classVal->outcome_like + 1);
                }
                $classVal->save();
                echo SkillConfig::STATUS_ACTIVE;
                exit();
            } else {
                $model->save();
                echo SkillConfig::STATUS_ACTIVE;
                exit();
            }
        } elseif ($_POST['classcoursetype'] == 'course') {
            $model->setAttribute('courses_id', $_POST['classcourseId']);
            $model->setAttribute('ip_address', $_SERVER['REMOTE_ADDR']);
            $courseVal = ClassCourseContentLikeUnlike::model()->findByAttributes(array('courses_id' => $_POST['classcourseId']));
            if (!empty($courseVal->courses_id)) {
                $courseVal->setAttribute('ip_address', $_SERVER['REMOTE_ADDR']);
                if ($_POST['type'] == SkillConfig::DESCRIPTION_CLASS_COURSE) {
                    ('description_like' != $description) ? $courseVal->setAttribute('description_unlike', $courseVal->description_unlike + 1) : $courseVal->setAttribute('description_like', $courseVal->description_like + 1);
                }
                if ($_POST['type'] == SkillConfig::STRUCTURE_CLASS_COURSE) {
                    ('structure_like' != $structure) ? $courseVal->setAttribute('structure_unlike', $courseVal->structure_unlike + 1) : $courseVal->setAttribute('structure_like', $courseVal->structure_like + 1);
                }
                if ($_POST['type'] == SkillConfig::OUTCOME_CLASS_COURSE) {
                    ('outcome_like' != $outcome) ? $courseVal->setAttribute('outcome_unlike', $courseVal->outcome_unlike + 1) : $courseVal->setAttribute('outcome_like', $courseVal->outcome_like + 1);
                }
                $courseVal->save();
                echo SkillConfig::STATUS_ACTIVE;
                exit();
            } else {
                $model->save();
                echo SkillConfig::STATUS_ACTIVE;
                exit();
            }
        }
    }

}